<%@page import="org.dom4j.io.*,org.dom4j.*, java.io.*,java.util.*" contentType="text/xml;charset=UTF-8"%>
<%!public AbstractMap<String, String> parseXml(String xml)
{
	try
	{
		ByteArrayInputStream byteIs = new ByteArrayInputStream(xml.getBytes("UTF-8"));
		SAXReader reader  = new SAXReader();
		Document document = reader.read(byteIs);

		AbstractMap<String, String> parameterMap = new HashMap<String, String>();
		
		treeWalk(document.getRootElement(), parameterMap);
		
		return parameterMap;
	}
	catch(DocumentException de){ de.printStackTrace(); }
	catch (UnsupportedEncodingException e){e.printStackTrace(); }
	
	return null;
}

private void treeWalk(Element element, AbstractMap<String, String> map)
{
	for(int i = 0, size = element.nodeCount(); i< size; i++)
	{
		Node node = (Node) element.node(i);
		if(node instanceof Element)
			treeWalk((Element) node, map);
		else
		{
			String name = node.getParent().getQName().getName();
			map.put(name, node.getText());
			System.out.println("=============name============" + name);
			System.out.println("=============text============" + node.getText());
		}
	}
}%>



<%
String mtId =null;
String statusCode=null;
//testing implementation***********888
/*
			String hubxml="<?xml version='1.0'?>"+
			"<MT>  "+
			"	<USER_NAME>324</USER_NAME>  "+
			"	<PASSWORD>423</PASSWORD>  "+
			"	<TRX_ID>1880914</TRX_ID>	<!-- MTDetail.hubs_mt_id -->"+
			"	<MSISDN>+32476957733</MSISDN>  "+
			"	<PRICE>0</PRICE>  "+
			"	<DCS>7b</DCS>  "+
			"	<ACK_LEVEL>1</ACK_LEVEL>  "+
			"	<ACK_URL>http://mthubresponse2.funmobilebe.com/MTResponse</ACK_URL>  "+
			"	<OPERATOR_ID>3202</OPERATOR_ID>  "+
			"	<TEXT>Nog steeds geinteresseerd in jouw item? Stuur nu"+
			"YES naar 3377. Help: 02-6260025</TEXT>"+
			"	<CONTENT_TYPE_ID>117</CONTENT_TYPE_ID>"+
			"	<PRIORITY>0</PRIORITY>"+
			"</MT>";
*/
//******************
			
			
			
//reall implementation*********
BufferedReader br = request.getReader();//new BufferedReader(new InputStreamReader(request.getInputStream()));
String readLine   = null;	
String hubxml     = "";

//read everything from the request, and put it into a string
while(br!=null && (readLine=br.readLine())!=null){
	hubxml += readLine;
	System.out.println("xml print out :"+hubxml);
}

//******************************
//prase the string into map
AbstractMap<String, String> map= null;
if(!hubxml.equals(""))
{	
	System.out.println(hubxml);
	System.out.println("run prase XML");
	map=parseXml(hubxml);
}

if(map!=null)
{
	System.out.println("is not null");
	mtId=map.get("TRX_ID");
	System.out.println(mtId);
	statusCode="1";//sent to hub and success
}

if(mtId==null || (mtId.equals("")))
{	
	mtId="-1";
	statusCode="-1";//default value
					//can also check if msisdn length is valid
					//-2 = invalid msisdn length
}
else
{
	statusCode="1";
}

try
{
	String xml="<?xml version='1.0' encoding='UTF-8'?><MT_RESP><MT_ID>"+mtId+"</MT_ID><STATUS_CODE>"+statusCode+"</STATUS_CODE></MT_RESP>";
	response.getWriter().write(xml);
	//System.out.println(xml);
}
catch(Exception err)
{
	response.getWriter().write(err.toString());
}
%>